package com.qiall.boot.userservice.Impl.organization;


import com.qiall.assertutil.Assert;
import com.qiall.boot.userservice.Impl.organization.activity.AddOrganizationCheckActivity;
import com.qiall.boot.userservice.Impl.organization.activity.DeleteOrgCheckActivity;
import com.qiall.boot.userservice.Impl.organization.activity.OrgConvert;
import com.qiall.boot.userservice.data.entity.OrganizationInfo;
import com.qiall.boot.userservice.data.mapper.OrganizationInfoMapper;
import com.qiall.common.entity.PageQuery;
import com.qiall.common.entity.PageResponse;
import com.qiall.common.exception.BusinessResult;
import com.qiall.common.request.BaseResponse;
import com.qiall.common.request.GetByIdRequest;
import com.qiall.user.ability.entity.AddOrganizationDTO;
import com.qiall.user.ability.entity.OrgPageQueryDTO;
import com.qiall.user.ability.entity.OrganizationResponse;
import com.qiall.user.ability.service.organization.OrganizationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴金辉
 */
@DubboService
@Component
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired
    private OrganizationInfoMapper organizationInfoMapper;
    @Autowired
    private OrgConvert orgConvert;

    @Autowired
    private AddOrganizationCheckActivity addOrganizationCheckActivity;
    @Autowired
    private DeleteOrgCheckActivity deleteOrgCheckActivity;

    @Override
    public BusinessResult<BaseResponse> addOrganization(AddOrganizationDTO request) {
        BaseResponse baseResponse = new BaseResponse();
        addOrganizationCheckActivity.handler(request, baseResponse);
        OrganizationInfo organizationInfo = orgConvert.trans(request);
        organizationInfoMapper.insert(organizationInfo);
        return BusinessResult.success(baseResponse);
    }

    @Override
    public BusinessResult<BaseResponse> deleteOrganization(GetByIdRequest request) {
        BaseResponse baseResponse = new BaseResponse();
        deleteOrgCheckActivity.handler(request, null);
        organizationInfoMapper.delete(request.getId());
        return BusinessResult.success(baseResponse);
    }

    @Override
    public BusinessResult<OrganizationResponse> getOrganizationInfo(GetByIdRequest getByIdRequest) {
        Assert.notNull(getByIdRequest.getId(), "id 不能为空");
        OrganizationInfo organizationInfo = organizationInfoMapper.select(getByIdRequest.getId());
        OrganizationResponse organizationResponse = orgConvert.trans(organizationInfo);
        return BusinessResult.success(organizationResponse);

    }

    @Override
    public BusinessResult<PageResponse<OrganizationResponse>> getOrganizationByPage(OrgPageQueryDTO request) {
        OrganizationInfo queryBean = new OrganizationInfo();
        queryBean.setOrgName(request.getOrgName());
        PageQuery pageQuery = request.getPageQuery();
        int count = organizationInfoMapper.count(queryBean, pageQuery.getStartTime(), pageQuery.getEndTime());
        if (count == 0) {
            BusinessResult.success(PageResponse.nullValue(pageQuery));
        }
        List<OrganizationInfo> organizationInfoList = organizationInfoMapper.selectByPage(queryBean, pageQuery.getStartTime(), pageQuery.getEndTime(), pageQuery.limit(), pageQuery.offset());
        List<OrganizationResponse> organizationInfoPageResp = organizationInfoList.stream().map(x -> orgConvert.trans(x)).collect(Collectors.toList());

        PageResponse pageResponse = PageResponse.withValue(pageQuery, count, organizationInfoPageResp);
        return BusinessResult.success(pageResponse);

    }
}
